package com.zcf.utils.rsa;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.lang.reflect.Type;

/**
 * @author jj
 * @date 2019/11/28 9:30
 */
public class RSADecodeUtils {
    /**
     * 获取公钥
     */
   private static String path = new Object() {
        public String getPath() {
            return this.getClass().getResource("/privateKey.properties").getFile();
        }
    }.getPath().substring(1);
    /**
    * @description :私钥解码
    * @param content 
    * @param clazz
    * @date : 2019/11/28 9:40
    * @author : jj
    */
    public static  <T> T decode(String content, Class<T> clazz){
        content = content.replaceAll("%2B","+");
        //获取私钥
        String privateKey ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJegIksAHRb3GAWQIK73qCx5nfKZ3N6jpzWQ3ueut+Rp5xBrBd6LbJ6TDMkztpW8cyYX4CCq31YkukM1RgSvrqVnw0UrEk6xnnv5wCwqy3dAES8qFRUPzbVifiHRXgZUSJUIcfysXGw5DPFrUlS0AuxnXZNPmXDsnP/dHAX8yMNnAgMBAAECgYA1rqFVknW6IsOjfJLFv7MQBALy2JzFSoWxxlfVCRvcR85iFrIeR5iuT/vP5Ho5a14NV5inmDe4RQqZLuxaTmTzaDsYeyClKJLR/0ur4uWIt5PKGGk52h4eDyID4eWNiOjAQHdIcz6Xik4fV5LyLyfFYXtkaLuaBI1yzPUf8TabuQJBAN9vHQkSe+ovqjFUksWAc92wrsy1mIcBv44eHUzHovPX/G10pLuwjMgnwq0bFj5zzWb0ze/m/jtKGqW3ogjMBoUCQQCtuaoAHYsSomouZDdRCu//e2onKf7MQM82DMXoAzanlr8kOVH0UqafNGdhCdh6xGTQ5HPng4ybjOoRAkvxAJP7AkEAh+2THnfORvRkd8pZ2CmDU7UvQn6folKf9JfBf3pcWjhl6zjxFvYbT5FQz/sQl0HgDeiUeIRw2iUE/RgwTWXB+QJAWJ8V8ekgQOlJcVD+RwFlXNcAV+o8aMXG7rOlYa4B+4wTBc+X1yMyjJSXCBGVtUrqgwWHetrIp7FC1lptkwvQcQJAKiVCkCMpO3XWit4XVvpg7UkyPfjocCadRzdCZjtc9qD3oFQxo30b51Q+50GNZKjAxo9aI/3g9Z1bJjhQ4YVyNw\\=\\=";
        byte[] decryptByPrivateKey = new byte[0];
        try {
            decryptByPrivateKey = RSAUtils.decryptByPrivateKey(Base64Utils.decode(content), privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //解密后的密码
        String decode= null;
        try {
            decode = new String(decryptByPrivateKey,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return JSONObject.parseObject(decode, (Type) clazz);
    }

    /**
     * @description :私钥编码
     * @param content
     * @param clazz
     * @date : 2019/11/28 9:40
     * @author : jj
     */
    public static  <T> T encode(String content, Class<T> clazz){
        content = content.replaceAll("%2B","+");
        //获取私钥
        String privateKey =readTxt(path);
        byte[] decryptByPrivateKey = new byte[0];
        try {
            decryptByPrivateKey = RSAUtils.encryptByPrivateKey(Base64Utils.decode(content), privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //解密后的密码
        String decode= new String(decryptByPrivateKey);

        return JSONObject.parseObject(decode, (Type) clazz);
    }

    /**
    * @description :获取私钥
    * @param filePath 
    * @date : 2019/11/28 11:57
    * @author : jj
    */
    public static String readTxt(String filePath) {
        String lineTxt = null;
        try {
            File file = new File(filePath);
            if(file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);

                while ((lineTxt = br.readLine()) != null) {
                    System.out.println(lineTxt);
                    return lineTxt;
                }
                br.close();
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }
        return lineTxt;

    }


    public static void main(String[] args) {
        System.out.println(path);
    }
}
